import React, { useEffect, useState } from 'react'
import { message, Modal, Form, Input, Select } from 'antd';
import { management_Add, Partition_Add ,Variety_Add,Specification_Add} from '../service';
const AddPartitionModel = ({ isModalOpen, setIsModalOpen,AddType,Management_Update,Partition_Update}) => {

    const [Label, setLabel] = useState({
        label: "",
        name: ""
    });


    useEffect(() => {
        console.log(AddType)
        if (AddType == "管理区分区") {

            setLabel({
                label: "管理区分区",
                name: "area_name"
            });
        }

        if (AddType == "新增物理分区") {

            setLabel({
                label: "物理分区",
                name: "zone_number"
            });
        }

    }, [])


    const [form] = Form.useForm();

    const handleSubmit = () => {
        form.validateFields().then(async values => {

            if (AddType == "管理区分区") {
                let res = await management_Add(values)
                if (res.code == 0) {
                    message.success("添加成功")
                    Management_Update()
                    setIsModalOpen(false)

                } else {
                    message.error("添加失败")
                }
            }

            if (AddType == "新增物理分区") {
                let res = await Partition_Add(values)
                if (res.code == 0) {
                    message.success("添加成功")
                    Partition_Update()
                    setIsModalOpen(false)

                } else {
                    message.error(res.msg)
                }
            }




        });
    };





    return (
        <div>

            <Modal
                title={AddType}
                closable={{ 'aria-label': 'Custom Close Button' }}
                visible={isModalOpen}
                onCancel={() => setIsModalOpen(false)}
                onOk={handleSubmit}
            >
                <Form
                    name="basic"
                    labelCol={{ span: 7 }}
                    wrapperCol={{ span: 16 }}
                    style={{ maxWidth: 600 }}
                    form={form}
                    autoComplete="off"
                >
                    <Form.Item
                        label={Label.label}
                        name={Label.name}
                        rules={[{ required: true, message: `请输入${AddType}` }]}
                    >
                        <Input />
                    </Form.Item>
                    {
                        AddType == "管理区分区" && <Form.Item
                            label="锁类型"
                            name="lock_type"
                            rules={[{ required: true, message: '请输锁类型' }]}
                        >
                           <Select
                                placeholder="请选择锁类型"
                               
                            > 
                            <Select.Option value="鞋柜">鞋柜</Select.Option>
                            <Select.Option value="更衣柜">更衣柜</Select.Option>
                            </Select>
                        </Form.Item>    
                    }





                </Form>
            </Modal>


        </div>
    )
}

export default AddPartitionModel